package com.ticket.sass.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.mapper.FilmMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.common.entity.tenant.FilmEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-03-07
 */
@Service
public class FilmServiceImpl extends ServiceImpl<FilmMapper, FilmEntity> implements BaseService<FilmEntity> {
    public Map<Long, List<FilmEntity>> queryFilmsByPriceSchemeIds(List<Long> priceSchemeIds) {
        if (priceSchemeIds == null || priceSchemeIds.isEmpty()) {
            return new HashMap<>();
        }
        
        // 将价格方案ID列表转换为逗号分隔的字符串
        String priceSchemeIdsString = String.join(",", priceSchemeIds.stream().map(String::valueOf).collect(Collectors.toList()));
        
        // 调用新的查询方法，接受一个字符串参数
        List<FilmEntity> films = this.baseMapper.queryFilmsByPriceSchemeIds(priceSchemeIdsString);
        
        // 构建结果映射
        Map<Long, List<FilmEntity>> resultMap = new HashMap<>();
        for (FilmEntity film : films) {
            Long priceSchemeId = film.getPriceSchemeId(); // Assuming there is a method to get the price scheme id from FilmEntity
            if (!resultMap.containsKey(priceSchemeId)) {
                resultMap.put(priceSchemeId, new ArrayList<>());
            }
            resultMap.get(priceSchemeId).add(film);
        }
        
        return resultMap;
    }
    
    public Map<Long, String> mapCoverByIds(List<Long> ids) {
        if (ids.size() == 0){
            return new HashMap<>();
        }
        
        QueryWrapper<FilmEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "cover")
            .in("id", ids);
        List<FilmEntity> filmEntities = this.baseMapper.selectList(queryWrapper);
        return filmEntities.stream().collect(Collectors.toMap(FilmEntity::getId, FilmEntity::getCover));
    }

    
    public List<FilmEntity> queryFilmsByCouponId(Long couponId){
        return this.baseMapper.queryFilmsByCouponId(couponId);
    }
}
